using System;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.RenderGraphModule;

namespace LiteRP
{
    
    public partial class LiteRenderGraphRecorder
    {
        private static readonly ProfilingSampler s_ClearRenterTargetProfilingSample = new ProfilingSampler("Clear Render Target");
        
        internal class ClearRenderTargetPass
        {
            internal RTClearFlags clearFlags;
            internal Color clearColor;
        }

        private void AddClearRenderTargetPass(RenderGraph renderGraph, CameraData cameraData)
        {
            using (var builder = renderGraph.AddRasterRenderPass<ClearRenderTargetPass>("Clear Render Target", out var passData, s_ClearRenterTargetProfilingSample))
            {
                passData.clearFlags = cameraData.GetClearFlags();
                passData.clearColor = cameraData.GetClearColor();

                // 设置渲染Backbuffer
                if (m_BackbuffColorTextureHandle.IsValid())
                {
                    builder.SetRenderAttachment(m_BackbuffColorTextureHandle, 0, AccessFlags.Write);
                }

                builder.AllowPassCulling(false);
                
                builder.SetRenderFunc((ClearRenderTargetPass data, RasterGraphContext context) =>
                {
                    context.cmd.ClearRenderTarget(data.clearFlags, passData.clearColor, 1, 0);
                });
            }
        }

    }
}